package com.mobile.ado.setting.ringtone;

import java.util.ArrayList;
import java.util.Calendar;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.mobile.ado.R;

public class RingtoneSettingTimerAdapter extends BaseAdapter {

	private ArrayList<TimeSpanRingtone> ringtones;
	private LayoutInflater inflater;

	public RingtoneSettingTimerAdapter(Context context,
			ArrayList<TimeSpanRingtone> ringtones) {
		inflater = LayoutInflater.from(context);

		this.ringtones = ringtones;

	}

	@Override
	public int getCount() {
		return ringtones.size();
	}

	@Override
	public Object getItem(int position) {
		return ringtones.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;

		if (convertView == null) {
			holder = new ViewHolder();

			convertView = inflater.inflate(R.layout.ringtone_timer_list_items,
					null);
			holder.fromTextView = (TextView) convertView
					.findViewById(R.id.from_time);
			holder.toTextView = (TextView) convertView
					.findViewById(R.id.to_time);
			holder.pathTextView = (TextView) convertView
					.findViewById(R.id.audio_path);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		TimeSpanRingtone ringtone = ringtones.get(position);

		Calendar startTime = ringtone.getStartTime();
		int hour = startTime.get(Calendar.HOUR);
		int minute = startTime.get(Calendar.MINUTE);
		int second = startTime.get(Calendar.SECOND);
		holder.fromTextView.setText(hour + ":" + minute + ":" + second);
		Calendar endTime = ringtone.getEndTime();
		hour = endTime.get(Calendar.HOUR);
		minute = endTime.get(Calendar.MINUTE);
		second = endTime.get(Calendar.SECOND);
		holder.toTextView.setText(hour + ":" + minute + ":" + second);
		String audioPath = ringtone.getAudioPath();
		holder.pathTextView.setText(audioPath);

		return convertView;
	}

}

class ViewHolder {
	TextView fromTextView;
	TextView toTextView;
	TextView pathTextView;
}
